﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;

namespace ConceptCorporateAdmin.Gui.Widgets
{
    public partial class DocumentEditor : UserControl
    {
        

        DbActNormativ actNormativ = null;

        public DocumentEditor()
        {
            InitializeComponent();
        }

        public DbActNormativ ActNormativ
        {
            get
            {
                return actNormativ;
            }
            set
            {
                actNormativ = value;
                ListaEntitati.Nodes.Clear();
                if (value != null)
                {
                    //daca nu exista codul sursa creeaza-l gol
                    string sursa = "database/files/" + ActNormativ.Id + "_sursa.htm";
                    if (!File.Exists(sursa))
                    {
                        File.CreateText(sursa).Close();
                    }
                    //afiseaza codul sursa
                    using (StreamReader sr = new StreamReader(sursa))
                        BEditor.DocumentText = sr.ReadToEnd();
                    //verifica daca exista tree-ul xml, daca nu exista creeaza-l gol
                    string xml = "database/files/" + ActNormativ.Id + ".xml";
                    if (!File.Exists(xml))
                    {
                        XmlDocument doc = new XmlDocument();
                        //adauga declaratia
                        XmlNode d = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                        doc.AppendChild(d);
                        //adauga root element
                        XmlNode root = doc.CreateElement("", "ROOT", "");
                        doc.AppendChild(root);
                        //
                        doc.Save(xml);
                    }
                    //afiseaza tree-ul
                    createTreeFromXml();
                }
            }
        }

        private void createTreeFromXml()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("database/files/" + ActNormativ.Id + ".xml");
            XmlNodeList lst = doc.ChildNodes;
            if (lst.Count == 2)
            {
                lst = doc.ChildNodes[1].ChildNodes;
                foreach (XmlNode node in lst)
                {
                    parseXmlNode(node, ListaEntitati.Nodes );
                }
            }
        }

        private void parseXmlNode( XmlNode node, TreeNodeCollection tnode_col )
        {
            if (node.NodeType != XmlNodeType.Text)
            {
                String text = node.ChildNodes[0].Value;
                String attr = node.Attributes["anchor"].Value;
                int index   = tnode_col.Add(new EntitateTreeNode(text, attr));
                TreeNodeCollection new_tnode_col = tnode_col[index].Nodes;
                foreach (XmlNode n in node.ChildNodes)
                {
                    parseXmlNode(n,new_tnode_col);
                }
            }
        }

        private void BtnGenerate_Click(object sender, EventArgs e)
        {
            if (BEditor.DocumentText != null)
            {
                string text = BEditor.DocumentText;
                Generator g = new Generator(ListaEntitati, BEditor.DocumentText, ActNormativ, PgProgres);
                g.generateTreeFromString();
                
            }
        }

        private void ListaEntitati_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                EntitateTreeNode nod = (EntitateTreeNode)ListaEntitati.SelectedNode;
                MessageBox.Show(nod.DataModificarii.ToString());
            }
            catch (Exception q) 
            {
                Console.WriteLine(q.Message + q.StackTrace);
            }
        }

    }
}
